package cn.com.callback

import cn.com.elite.EliteBean
import cn.com.elite.Util
import cn.com.kh.KHUtil
import cn.com.kh.signUtil
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 * 工单查询API
 */

public class apiGetWOList implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiGetWOList.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        long startTime = System.currentTimeMillis();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");

        // 初始化返回参数
        JSONObject result = new JSONObject();

        JSONObject resultJson = null;
        //接收客户端调用参数
        String callJson = "";
        InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
        BufferedReader iny = new BufferedReader(reader);
        String line;
        while ((line = iny.readLine()) != null) {
            callJson += line;
        }

        log.info("[调用请求信息]:" + callJson);

        JSONObject signJson = signUtil.CheckSign(JSONObject.parseObject(callJson));
        log.info("[签名验证结果]:");
        log.info(signJson);
        if (signJson != null) {
            return signJson.toString();
        } else {
            try {
                //解析Json
                JSONObject inJson = JSONObject.parseObject(callJson);
                "values (uuid(),in_workorder_guid,in_oldhandleby_id,in_oldhandlegroup_id,in_handleby_id,in_handlegroup_id,in_workorderstepstatus,in_comments,\n" +
                        "in_modifieddate,in_from_stepidmap,in_to_stepidmap,in_stepexpired,in_stepstart,in_cur_expiredlen,in_cur_expiredflag,in_workstarttime,in_workendtime)";
                //获取查询参数
                String c_user_id = inJson.getString("c_user_id");
                String in_workorder_guid = inJson.getJSONObject("data").getString("in_workorder_guid");// 工单id
                String in_oldhandleby_id = inJson.getJSONObject("data").getString("in_oldhandleby_id"); // 原处理人
                String in_oldhandlegroup_id = inJson.getJSONObject("data").getString("in_oldhandlegroup_id");// 原处理组
                String in_handleby_id = inJson.getJSONObject("data").getString("in_handleby_id"); // 新处理人
                String in_handlegroup_id = inJson.getJSONObject("data").getString("in_handlegroup_id");// 新处理组
                String in_workorderstepstatus = inJson.getJSONObject("data").getString("in_workorderstepstatus"); // 工单操作类型“保存：CLOSE,下送：SENDNS”
                String in_comments = inJson.getJSONObject("data").getString("in_comments"); // 步骤处理意见
                String in_modifieddate = inJson.getJSONObject("data").getString("in_modifieddate"); // 操作时间
                String in_from_stepidmap = inJson.getJSONObject("data").getString("in_from_stepidmap"); // 原工单节点
                String in_to_stepidmap = inJson.getJSONObject("data").getString("in_to_stepidmap"); // 新工单节点
                String in_stepexpired = inJson.getJSONObject("data").getString("in_stepexpired"); // 步骤过期时间
                String in_stepstart = inJson.getJSONObject("data").getString("in_stepstart"); // 步骤开始时间
                String in_cur_expiredlen = inJson.getJSONObject("data").getString("in_cur_expiredlen"); // 当前工单过期时长单位为s
                String in_cur_expiredflag = inJson.getJSONObject("data").getString("in_cur_expiredflag"); // 当前工单是否过期标志 1为已过期 0为未过期
                String in_workstarttime = inJson.getJSONObject("data").getString("in_workstarttime"); // 当前步骤操作开始时间
                String in_workendtime = inJson.getJSONObject("data").getString("in_workendtime"); // 当前步骤操作结束时间

                log.debug("请求参数-操作人信息-c_user_id:" + c_user_id);

                // 查询判断当前这个操作人是客户还是员工
                String c_user_type = "";
                // 客户信息
                String Customer_Guid = null;
                String Loginname = null;

                // 员工信息
                String Rolegroup_Id = null;
                String Staff_Id = null;
                List selectResult = eliteBean.chaxun_sql("Select R2.Rolegroup_Id, R1.Staff_Id, R2.Rolegroupdescription, R1.Staffname" +
                        "  From Staff R1" +
                        "  Left Join Rolegroup R2" +
                        "    On R1.Defaultrolegroup = R2.Rolegroup_Id" +
                        " Where R1.Workstatus = 'ONLINE'" +
                        "   And R1.Openid = '" + c_user_id + "' Limit 1", "dataSource");

                if (selectResult.size() <= 0) {
                    List selectResultc = eliteBean.chaxun_sql("Select Customer_Guid, Customername, Mobile, Loginname From Customer Where Open_Id = '" + c_user_id + "' Limit 1", "dataSource");
                    if (selectResultc.size() <= 0) {

                        result.put("code", 1007);
                        result.put("msg", "当前操作人非法！");
                        return result;

                    } else {
                        Map selectResultcm = selectResultc.get(0);
                        Customer_Guid = selectResultcm.get("Customer_Guid");
                        Loginname = selectResultcm.get("Loginname");
                        String Customername = selectResultcm.get("Customername");
                        String Mobile = selectResultcm.get("Mobile");
                        log.debug("当前操作客户信息-Customer_Guid:" + Customer_Guid + " Customername:" + Customername + " Mobile:" + Mobile + " Loginname:" + Loginname);
                        c_user_type = "customer";

                    };
                } else {
                    Map selectResultm = selectResult.get(0);
                    Rolegroup_Id = selectResultm.get("Rolegroup_Id");
                    Staff_Id = selectResultm.get("Staff_Id");
                    String Rolegroupdescription = selectResultm.get("Rolegroupdescription");
                    String Staffname = selectResultm.get("Staffname");
                    log.debug("当前操作员工信息-Rolegroup_Id:" + Rolegroup_Id + " Rolegroupdescription:" + Rolegroupdescription + " Staff_Id:" + Staff_Id + " Staffname:" + Staffname);
                    c_user_type = "staff";

                };

                String insertSQL = "insert into workorderstep3(workorderstep_guid,workorder_guid,oldhandleby_id,oldhandlegroup_id,handleby_id,handlegroup_id,workorderstepstatus,comments,\n" +
                        "modifieddate,from_stepidmap,to_stepidmap,stepexpired,stepstart,cur_expiredlen,cur_expiredflag,workstarttime,workendtime) \n" +
                        "values (uuid(),in_workorder_guid,in_oldhandleby_id,in_oldhandlegroup_id,in_handleby_id,in_handlegroup_id,in_workorderstepstatus,in_comments,\n" +
                        "in_modifieddate,in_from_stepidmap,in_to_stepidmap,in_stepexpired,in_stepstart,in_cur_expiredlen,in_cur_expiredflag,in_workstarttime,in_workendtime)";

                log.debug("执行workorderstep数据插入SQL:" + insertSQL);
                eliteBean.update(insertSQL,'dataSource');

                String reJoson = Util.getJsonFromList(WorkOrderList);
                reJoson=!KHUtil.isNull(reJoson)?reJoson:"[]";
                long endTime = System.currentTimeMillis();
                result.put("code", 1);
                result.put("msg", "处理完成、调用问题处理时长(毫秒):" + (endTime - startTime));
                JSONArray dataArr = JSONArray.parseArray(reJoson);
                result.put("data", dataArr);

            } catch (Exception e) {
                result.put("code", 1008);
                result.put("msg", e.getMessage());
            }

        }

        return result.toString();
    }
}